<template>
  <div class="app-container">
    <el-form
      ref="ruleForm"
      :inline="true"
      :model="form"
      class="demo-form-inline filter-container"
    >
      <el-form-item label="信件类别" prop="letterType" >
        <el-select v-model="form.letterType" @change="checkType">
          <el-option
            v-for="item in consts.LETTER_TYPE"
            :key="item.value"
            :label="item.title"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="细分类别" prop="type" >
        <el-select v-model="form.type" :disabled="!isSystem">
          <el-option
            v-for="item in consts.LETTER_SUB_TYPE"
            :key="item.value"
            :label="item.title"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标题：" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="发件人：" prop="sendName">
        <el-input v-model="form.sendName"></el-input>
      </el-form-item>
      <el-form-item label="是否已读"  prop="isRead">
        <el-select v-model="form.isRead">
          <el-option
            v-for="item in statusArray"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发送时间：" prop="time">
        <el-date-picker
          v-model="form.time"
          class="filter-item"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
          value-format="yyyy-MM-dd HH:mm:ss"
          align="left"
        ></el-date-picker>
      </el-form-item>

      <el-button type="primary" v-waves icon="el-icon-search" @click="submitFilterForm">搜索</el-button>
      <el-button @click="selfResetForm('ruleForm')">重置</el-button>
    </el-form>
    <!--v-if="roleCheck('feedback-type-add')"-->
    <el-table
      :data="tableData.list"
      v-loading="loading"
      element-loading-text="给我一点时间"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="信件类别" align="center" prop="letterType">
        <template slot-scope="{row}">{{row.letterType | toConverter('LETTER_TYPE')}}</template>
      </el-table-column>
      <el-table-column label="细分类别" align="center" prop="type">
        <template slot-scope="{row}">{{row.type | toConverter('LETTER_SUB_TYPE')}}</template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title"></el-table-column>
      <el-table-column label="发送时间" align="center" prop="createTime">
        <template slot-scope="{row}">{{row.createTime | toTime}}</template>
      </el-table-column>
      <el-table-column label="发件人" align="center" prop="sendName">
        <template slot-scope="{row}">{{row.sendName}}</template>
      </el-table-column>
      <el-table-column label="是否已读" align="center" prop="isRead">
        <template slot-scope="{row}">{{row.isRead==0?'未读':'已读'}}</template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="300px">
        <!--v-if="roleCheck('letter-summary-detail')"-->
        <template slot-scope="{row}">
          <el-button
            type="text"
            v-if="roleCheck('mail-letter-check')"
            @click="openDialog({dialogName: 'letter-detail',
                     props: row
                     })"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="gridSizeChange"
        @current-change="gridCurrentChange"
        :current-page="filters.page"
        :page-sizes="gridPageSize"
        :page-size="filters.size"
        :layout="gridLayout"
        :total="tableData.total"
      ></el-pagination>
    </div>

    <el-dialog
      :visible.sync="dialogVisible"
      :before-close="() => {dialogVisible = false}"
      append-to-body
    >
      <letter-detail
        style="max-height:60vh; overflow: auto"
        :id="dialogProps.noticeId"
        :row-data="dialogProps"
        @on-success="dialogVisible = false;"
        v-if="dialogName === 'letter-detail'"
      ></letter-detail>
    </el-dialog>
  </div>
</template>

<script>
import {
  form, resetForm, grid, dialog,
} from '@/build';
import LetterDetail from './letterDetail';
import { userLetterListApi } from '@/services/api/letterSummary';

export default {
  name: 'letter-list',

  mixins: [form, resetForm, grid, dialog],

  components: {
    LetterDetail,
  },

  props: {
    rowData: Object,
  },

  data() {
    return {
      isSystem: false, // 是否为系统消息
      time: [
        moment()
          .startOf('month')
          .format('YYYY-MM-DD 00:00:00'),
        moment().format('YYYY-MM-DD 23:59:59'),
      ],
      operatorList: [],
      statusArray: [
        {
          value: '',
          name: '全部',
        },
        {
          value: 0,
          name: '未读',
        },
        {
          value: 1,
          name: '已读',
        },
      ],
      tableData: {
        list: [],
        total: 0,
      },
      form: {
        userId: this.rowData.userId,
        letterType: undefined,
        type: undefined,
        title: undefined,
        sendName: undefined,
        isRead: undefined,
        time: [
          moment().format('YYYY-MM-DD 00:00:00'),
          moment().format('YYYY-MM-DD 23:59:59'),
        ],
      },
      filters: {
        userId: this.rowData.userId,
        letterType: undefined,
        type: undefined,
        title: undefined,
        sendName: undefined,
        isRead: undefined,
        beginTime: undefined,
        endTime: undefined,
        page: 1,
        size: 20,
      },
    };
  },

  methods: {
    checkType(val) {
      if (val == 3) {
        this.isSystem = true;
      } else {
        this.isSystem = false;
        this.form.type = undefined;
      }
    },
    submitFilterForm() {
      const { time } = this.form;
      let beginTime;
      let endTime;
      if (!_.isUndefined(time) && _.isArray(time)) {
        beginTime = time[0];
        endTime = time[1];
      }
      this.filters = { ...this.form, beginTime, endTime };
      this.gridSearch();
    },
    selfResetForm(formName) {
      this.isSystem = false;
      this.resetForm(formName);
      this.submitFilterForm();
    },
  },

  mounted() {
    this.bindGet(userLetterListApi, 'tableData', {
      action: 'getGrid',
      sendingData: 'filters',
      // beforeSend: data => {
      //   this.time = this.time || ["", ""];
      //   let beginTime = this.time[0];
      //   let endTime = this.time[1];
      //   return { ...this.filters, beginTime, endTime };
      // }
    });
    this.getAll();
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  height: 1300px;
  overflow: auto;
}
</style>
